<template>
  <div class="main-container">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>合同管理</el-breadcrumb-item>
      <el-breadcrumb-item>查看合同</el-breadcrumb-item>
    </el-breadcrumb>
   <!-- 卡片 -->
<el-card class="box-card">
  <el-row :gutter="30">
      <!-- 搜索 -->
    <el-col :span="8">
      <el-input placeholder="请输入内容" v-model="queryInfo.query" @keyup.enter.native="getContactsList" clearable @clear="getContactsList">
        <el-button slot="append" icon="el-icon-search" @click="getContactsList"></el-button>
      </el-input>
    </el-col>
    <el-col :span="4">
    </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="dialogVisible=true">
          新建合同
        </el-button>
      </el-col>
</el-row>
<!-- 表格 -->
<el-table :data="contractsList" style="width: 100%" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="contractName" label="合同名称" ></el-table-column>
      <el-table-column prop="launchUser" label="发起人" width='70'></el-table-column>
      <el-table-column prop="receiveUser" label="签署人" width='70'></el-table-column>
      <el-table-column prop="launchTime" label="签署发起时间">
        <template slot-scope='scope'>
          {{scope.row.launchTime|dateFormat}}
        </template>
      </el-table-column>
      <el-table-column prop="ddl" label="签署截止时间" >
        <template slot-scope='scope'>
          {{scope.row.ddl|dateFormat}}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" >
        <template slot-scope="scope">
          <el-tag
            :type="getStatus(scope.row.status).color"
            disable-transitions>{{getStatus(scope.row.status).text}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column  label="操作" width="130">
        <template slot-scope="scope">
         <el-button type="primary" size="mini" @click="addToCar(scope.row)" class="el-icon-document-add">

         </el-button>
         <el-button type="danger" size="mini" @click="removeById(scope.row)" class="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
<!--    <el-pagination-->
<!--        @size-change="handleSizeChange"-->
<!--        @current-change="handleCurrentChange"-->
<!--        :current-page="queryInfo.pagenum"-->
<!--        :page-sizes="[1, 2, 5, 10]"-->
<!--        :page-size="queryInfo.pageSize"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--        :total="total">-->
<!--      </el-pagination>-->
    </el-card>
<!-- 添加待入职用户 -->
    <el-dialog
      title="添加待入职用户"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addClose"
    >
      <el-row>
        添加合同名、接收人等信息
      </el-row>
      <el-form  label-width="70px" :model="addForm"  ref="addForm">
        <el-form-item label="接收人邮箱" prop="email" :disabled="true">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="接收人姓名" prop="name" :disabled="true">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="addForm.contractName"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addContract">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data(){
    return {
      queryInfo:{
        query:'',
        pagenum:1,
        pagesize:10
      },
      contractsList:[],
      total:0,
      dialogVisible:false,
      addForm:{
        name:'',
        email: '',
        contractName:''
      },
    }
  },
  created(){
    this.getContactsList()
  },

  methods:{
    // 对话框关闭事件
    addClose:function(){
      this.$refs.addForm.resetFields();
    },
    addContract: async function() {
      const { data: res } = await this.$http.post('/contract/createContract', this.addForm)
      if (res.code !== 0) {
        this.$message.error("添加失败");
      }
      this.$message.success(res.data);
      this.dialogVisible = false;
      await this.getUserList();

    },
    getContactsList:async function(){
      const {data:res}=await this.$http.post('/contract/getAllContracts')
        if(res.code!==0){
          return this.$message.error("获取失败")
        }
      this.contractsList=res.data
      console.log(this.contractsList)
      this.$message.success("获取合同列表成功")
    },
    getStatus(val){
      let statusMap = {
        '0':{color:'warning',text:'待签署'},
        '1':{color:'success',text:'已签署'},
        '2':{color:'danger',text:'已过期'},
        '3':{color:'danger',text:'已解约'}
      }
      return statusMap[val]
    },
    // 获取最新的pagesize
    handleSizeChange:function(newsize){
        this.queryInfo.pagesize=newsize
        this.getContactsList()
    },
    handleCurrentChange:function(newPage){
      this.queryInfo.pagenum=newPage
      this.getContactsList()
    },
    removeById:async function(id){
      // 确认对话框
      const confirmResult=await this.$confirm("此操作将永久删除,是否继续?",'删除商品',{
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
      }).catch(err=>err)
      if(confirmResult!=='confirm'){
        return this.$message.info('已取消')
      }
      const {data:res}=await this.$http.delete(`goods/${id}`)
      console.log(res)
        if(res.data.status!==200){
        return this.$message.error("删除失败")
      }
      this.$message.success("删除成功")
      this.getContactsList()
    },
    addToCar:async function(good) {
      console.log(good,'good')
      const { data: res } = await this.$http.get(`shop/addCommToCar?commNames=${good.name}&uid=${good.commId}`)
      console.log(res)
      if (res.data.status !== 200) {
        return this.$message.error("购买失败")
      }
      this.$message.success("购买成功!!")
    }
  }
}
</script>
<style lang="less">
  .main-container{
    padding: 10px 20px;
    h4{
      color: gray;
      font-size:20px;
      font-weight: normal
    }
  }
</style>
